<?php
// +----------------------------------------------------------------------
// | huicmf [ huicmf快速开发框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2024 https://xiaohuihui.cc All rights reserved.
// +----------------------------------------------------------------------
// | Author: 小灰灰 <762229008@qq.com>
// +----------------------------------------------------------------------
// | Info:
// +----------------------------------------------------------------------
declare(strict_types=1);

namespace app\admin\controller;

use think\facade\View;
use app\common\model\LinksGroup as LinksGroupModel;

class LinksGroup extends Base
{

    /**
     * 分组列表
     * @return string
     */
    public function index(): string
    {
        if ($this->request->isAjax()) {
            $page  = (int)$this->request->param('page', 1);
            $limit = (int)$this->request->param('limit', 10);
            $data  = LinksGroupModel::getList($page, $limit);
            $count = LinksGroupModel::getCount();
            $this->success('ok', $data, $count);
        }

        return View::fetch();
    }

    public function add(): string
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (empty($param['title'])) {
                $this->error('分组名称不能为空');
            }
            $find = LinksGroupModel::where(['title' => $param['title']])->count();
            if ( ! empty($find)) {
                $this->error('分组名称已存在');
            }
            try {
                LinksGroupModel::create($param);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("操作成功");
        }

        return View::fetch();
    }

    public function edit(): string
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (empty($param['id'])) {
                $this->error('参数错误1');
            }
            if (empty($param['title'])) {
                $this->error('分组名称不能为空');
            }
            $find = LinksGroupModel::where(['title' => $param['title']])->where('id', '<>', $param['id'])->count();
            if ( ! empty($find)) {
                $this->error('分组名称已存在');
            }
            try {
                LinksGroupModel::update($param, ['id' => $param['id']]);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success('操作成功');
        }
        $id = $this->request->get('id');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $data = LinksGroupModel::find($id);
        if (empty($data)) {
            $this->error('获取数据失败');
        }
        View::assign('data', $data);

        return View::fetch();
    }

    public function delete(): string
    {
        if ($this->request->isAjax()) {
            $id = (int)$this->request->param('id');
            if (empty($id)) {
                $this->error("参数错误");
            }
            LinksGroupModel::destroy($id);
            $this->success("删除成功");
        }
        $this->error("请求错误");
    }
}
